package com.zygh.lz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zygh.lz.dao.*;
import com.zygh.lz.entity.EfficiencyRate;
import com.zygh.lz.entity.Gps;
import com.zygh.lz.entity.GpsTest;
import com.zygh.lz.service.GpsService;
import com.zygh.lz.util.DataTime;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Map;

@Service
public class GpsServiceImpl extends ServiceImpl<GpsMapper, Gps> implements GpsService {
    @Resource
    private GpsMapper gpsMapper;

    @Resource
    private GpsTestMapper gpsTestMapper;


    @Resource
    private PatrolrecordMapper patrolrecordMapper;
    @Resource
    private SignMapper signMapper;
    @Resource
    private SignStatisticsMapper signStatisticsMapper;
    @Resource
    private EfficiencyRateMapper efficiencyRateMapper;

    @Resource
    private BrigadeRecordMapper brigadeRecordMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private SectionMapper sectionMapper;


    /**
     * 新增GPS
     *
     * @param gps
     * @return
     */
    @Override
    public void addGps(Gps gps) {
        String[] split = gps.getCoordinate().split(",");
        gps.setGpsX(split[split.length - 2]);
        gps.setGpsY(split[split.length - 1]);
        gpsMapper.insertSelective(gps);

    }

    @Override
    public void addGpes(GpsTest gps) {
        String[] split = gps.getCoordinate().split(",");
        gps.setGpsX(split[split.length - 2]);
        gps.setGpsY(split[split.length - 1]);
        gpsTestMapper.insert(gps);

    }


    //轨迹统计
    public Map findGpsStatistics(int id, String startDate, String endDate) {
        DecimalFormat dub = new DecimalFormat("#0.0");
        if (StringUtils.isEmpty(startDate) && StringUtils.isEmpty(endDate)) {
            //默认取一周
            startDate = DataTime.getStartDate();
            endDate = DataTime.getEndDate();
        }

        Map<String, Object> map1 = signStatisticsMapper.fingSignStationByDuty(id, startDate, endDate);
        Double doubleValueObject = Double.valueOf(map1.get("hours").toString());
        //将case双精度型转换为int
        int hour1 = doubleValueObject.intValue();
        Double doubleValuee = Double.valueOf(map1.get("minutes").toString());
        int minute = doubleValuee.intValue();

        int duration = hour1 + minute;
        map1.put("duration", duration / 60 + " 小时" + duration % 60 + " 分钟");
        int hour = 0;
        if (duration > 0) {
            hour = duration / 60;
        }
        //活跃度=总里程/总时长
        double mileage = Double.valueOf(map1.get("totalDistance").toString());
        double liveness = 0;
        if (mileage == 0 || hour == 0) {
            map1.put("liveness", "0");
        } else {
            liveness = mileage / hour;
            map1.put("liveness", dub.format(liveness));
        }
        EfficiencyRate efficiencyRate = efficiencyRateMapper.selectOne(Wrappers.<EfficiencyRate>lambdaQuery().eq(EfficiencyRate::getId, 1));
        int duirate = efficiencyRate.getLivenessThreshold();
        if (liveness > duirate) {
            map1.put("activity", "高");
        } else if (liveness > duirate / 2) {
            map1.put("activity", "中");
        } else {
            map1.put("activity", "低");
        }

        return map1;
    }


}
